package com.ruoyi.web.controller.topic;

import com.ruoyi.system.domain.Message;
import com.ruoyi.system.domain.vo.Focus;
import com.ruoyi.system.domain.vo.MessageVo;
import com.ruoyi.system.domain.vo.Results;
import com.ruoyi.system.service.MessageService;
import com.ruoyi.web.controller.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class MessageController {

    @Autowired
    private MessageService messageService;

    @GetMapping("/api/getMessage/{user}/{other}")
    public Results selectMessageUserSendOther(@PathVariable("user") Long user,@PathVariable("other") Long other){
        List<Message> messages = messageService.selectUserSendOther(user, other);
        if(messages.isEmpty()){
            return new Results(500,"查询失败!",null);
        }
        return new Results(200,"查询成功!",messages);
    }

    @GetMapping("/api/getNoReadMessageCount/{user}")
    public Results getNoReadMessageCount(@PathVariable("user") Long user){
        List<MessageVo> messageVos = messageService.selectNoReadMessage(user);
        if(messageVos.isEmpty()){
            return new Results(500,"查询失败!",null);
        }
        return new Results(200,"查询成功!",messageVos);
    }

    @PostMapping("/api/updateMessageStatus")
    public Results updateMessageStatus(@RequestParam("user") Long user,@RequestParam("other") Long other){
        int i = messageService.updateMessageStatus(user, other);
        if(i==1){
            return new Results(500,"更改失败!",null);
        }
        return new Results(200,"更改成功!",null);
    }

    @DeleteMapping("/api/deleteForListId")
    public Results deleteForListId(@RequestParam("user") Long user,@RequestParam("other") Long other){
        System.out.println("asfddddddddd      "+user);
        System.out.println(other);
        int i = messageService.deleteForListId(user, other);
        if(i==1){
            return new Results(500,"删除失败!",null);
        }
        return new Results(200,"删除成功!",null);
    }

    @PostMapping("/api/createInformationBox")
    public Results createInformationBox(@RequestBody Focus focus){
        int informationBox = messageService.createInformationBox(focus, focus.getFocusid());
        if(informationBox==1){
            return new Results(500,"已经存在!",null);
        }
        return new Results(200,"添加成功!",null);
    }

    @GetMapping("/api/getInformationBox/{user}")
    public Results getInformationBox(@PathVariable("user") Long user){
        Map<String, Object> informationBox = messageService.getInformationBox(user);
        if(informationBox.isEmpty()){
            return  new Results(500,"没有查找到!",null);
        }
        return new Results(200,"查找成功!",informationBox);
    }

    @PostMapping("/api/insertMessage")
    public Results insertMessage(@RequestBody Message message){
        Message messages = new Message(null, message.getUser(), message.getOther(), message.getMage(), null,0);
        int i = messageService.insertMessage(messages);
        if(i==1){
            return new Results(500,"添加失败",null);
        }
        return new Results(200,"添加成功",null);
    }
}
